package game.module.qzmj.processor;

import java.util.List;
import java.util.concurrent.TimeUnit;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import game.RoomServer;
import game.common.PlayingRoleMsgProcessor;
import game.entity.PlayingRole;
import game.module.qzmj.bean.QzmjRound;
import game.module.qzmj.logic.QzmjDatamanager;
import game.module.qzmj.logic.QzmjLogic;
import game.session.SessionManager;
import lion.common.MsgCodeAnn;
import lion.netty4.message.MyRequestMessage;
import lion.netty4.message.RequestMessageRaw;
import lion.netty4.message.RequestProtoMessage;
import lua.LuaMessageQzmj.PushQzmjNewGame;
import lua.LuaMessageQzmj.PushQzmjNextRound;
import lua.LuaMessageQzmj.C2SQzmjNextRound;

@MsgCodeAnn(msgcode = C2SQzmjNextRound.id,accessLimit = 500)
public class QzmjNextRoundProcessor  extends PlayingRoleMsgProcessor{

	private static Logger logger = LoggerFactory.getLogger(QzmjNextRoundProcessor.class);
	
	@Override
	public void process(PlayingRole playingRole, RequestMessageRaw requestMessage) throws Exception {
		int playerId = playingRole.getId();
		final QzmjRound mjround = QzmjDatamanager.getInstance().getMjRoom(playerId);
		if(mjround==null)
			return;
		int m_wCurrentUser = mjround.getPlayerIds().indexOf(playerId);
		boolean palyRead[] = mjround.getPlayReady();
		List<Integer> plays = mjround.getPlayerIds();
		
		if (palyRead[m_wCurrentUser] == true) {
			return;// 该玩家已经准备好了
		}
		PushQzmjNextRound push = new PushQzmjNextRound();
		push.player_index = (byte) plays.indexOf(playerId);
		for (Integer aPlayerId : plays) {
			PlayingRole pr = SessionManager.getInstance().getPlayer(aPlayerId);
			if (pr != null && pr.isChannelActive()) {
				pr.writeAndFlush(push.build(pr.alloc()));
				// 推送关于出牌的信息
			}
		}
		palyRead[m_wCurrentUser] = true;
		for (boolean flag : palyRead) {
			if (flag == false)
				return;
		}
		QzmjLogic.getInstance().nextRound(mjround);

		if (mjround.getRoundSum() > 0) {
			QzmjLogic.getInstance().randCard(mjround);
			byte[][] cards = mjround.getCards();
			PushQzmjNewGame pushMsg = new PushQzmjNewGame();
			for (Integer aPlayerId : plays) {
				PlayingRole pr = SessionManager.getInstance().getPlayer(aPlayerId);
				if (pr != null && pr.isChannelActive()) {
					pushMsg.cards = cards[plays.indexOf(aPlayerId)];
				//	logger.info("cardds[{}]={}", plays.indexOf(aPlayerId), pushMsg.cards);
					pr.writeAndFlush(pushMsg.build(pr.alloc()));
					// 推送关于出牌的信息
				}
			}
		}
		// 下一轮发牌
		if (mjround.getRoundSum() > 0) {
			RoomServer.executorService.schedule(new Runnable() {

				@Override
				public void run() {

					QzmjLogic.getInstance().GameStart(mjround);
				}
			}, 2, TimeUnit.SECONDS);
		} else {
			for (Integer aPlayerId : plays) {
				QzmjDatamanager.getInstance().removePlayer(aPlayerId);
			}

			
			
			// mjround.set

		}
	}

	@Override
	public void processProto(PlayingRole playingRole, RequestProtoMessage request) throws Exception {
		
	}

	@Override
	public void processWebsocket(PlayingRole playingRole, MyRequestMessage request) throws Exception {
		
	}

}
